package com.example.lending.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lending.Model.User;
import com.example.lending.R;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
    private Context context;
    private List<User>users;
    private SharedPreferences sp;
    public UserAdapter(Context context, List<User> users) {
        this.context = context;
        this.users = users;
    }
    public void setAllUser(List<User> borrow){
        this.users=borrow;
    }
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        final User user = users.get(position);
        if(holder instanceof MyViewHolder){
            holder.tvName.setText("用户名："+user.getName());
            holder.tvSex.setText("性别："+user.getSex());
            holder.tvTel.setText("用户邮箱："+user.getEmail());
            holder.tvMajor.setText("专业名称："+user.getMajor());
            holder.tvClass.setText("班级信息："+user.getClassnum());
            holder.userid.setText("id:"+user.getId());
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {//跳转没有写
                    sp=context.getSharedPreferences("adapter",context.MODE_PRIVATE);
                    sp.edit().putString("username",user.getName()).commit();
                    NavController controller= Navigation.findNavController(v);
                    controller.navigate(R.id.action_allUserFragment_to_updataUserFragment2);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(users==null)
            return 0;
        return users.size();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user,parent,false);
        return new MyViewHolder(view);
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tvName,tvSex,tvMajor,tvTel,tvClass;
        TextView userid;
        Button button;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName=itemView.findViewById(R.id.tv_nameuser);
            tvSex=itemView.findViewById(R.id.tv_sexuser);
            tvMajor=itemView.findViewById(R.id.tv_majoruser);
            tvTel=itemView.findViewById(R.id.tv_teluser);
            tvClass=itemView.findViewById(R.id.tv_classuser);
            button=itemView.findViewById(R.id.btn_bianjiuser);
            userid=itemView.findViewById(R.id.userid);
        }
    }
}
